// pages/gradeData/gradeData.js
var app = getApp();
var name;
var id;
import choose from '../../api/choose.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    open: false,
    classIndex:-1,
    className:"初三一班",
	classlist:[],
	classList:{},
	page:1,
	size:10,
	count:0,
	loadingText:'',
  },
  //班级
  classlist(){
	 const that = this
	 choose.classlist().then(res =>{
		  if(res.data.errno === 0){
			  that.setData({
				  classlist:res.data.data.list
			  })
		} 
	  })
  },
  //获取统计数据列表
  chooselist(data){
  	  const that = this
  	  choose.classroom(data).then(res =>{
  		  if(res.data.errno === 0){
  			  that.setData({
  				  count:res.data.data.total,
  				  classList:res.data.data.list,
				  className:res.data.data.list.className
  			  })
  			  if(that.data.count===0 || that.data.classList.length === 0){
  				  that.setData({
  					loadingText:"~数据加载完毕~"
  				  })
  			  }else if(that.data.count===that.data.classList.length){
  					that.setData({
  						loadingText:"~数据加载完毕~"
  					})
  			  }
  		  }
  	  })
  },
  // 返回
  goBack(e) {
    wx.navigateBack({
      delta: e.currentTarget.dataset.index
    })
  },
  // 点击跳转页面
  openTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  // 确定选择班级
  onSureBtn(){
    this.data.open ? this.setData({ open: false }) : this.setData({ open: true });
    this.setData({ className: name })
	if(id){
		let data = {
			  classId:id,
			  page:this.data.page,
			  size:this.data.size
		  }
		this.chooselist(data)
	}
	
  },
    
  // 选择班级
  chooseClass(e){
    this.setData({
      classIndex: e.currentTarget.dataset.index,
    })
    name = e.currentTarget.dataset.title
	id = this.data.classlist[e.currentTarget.dataset.index].id
  },
  //查看学生
  chooseStudent(e){
	  console.log(e.currentTarget.dataset.menuitem.id)
	  wx.navigateTo({
		url:'/pages/index/index?id='+e.currentTarget.dataset.menuitem.id+'&updatatype=3'
	  })
  },
  //列表的操作函数
  open_list: function (opts) {
    this.setData({ open: false });
  },
  //左侧导航的开关函数
  off_canvas: function () {
    this.data.open ? this.setData({ open: false }) : this.setData({ open: true });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	let data = {
		  page:this.data.page,
		  size:this.data.size
	  }
	this.chooselist(data)
	this.classlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let len = this.data.classList.length;
    if(len>=this.data.count){
    	return false;
    }else{
    	this.setData({
    		page:this.data.page+1,
    		loadingText:"正在加载中..."
    	})
    	let data = {
    	  page:this.data.page,
    	  size:this.data.size
    	}
    	this.chooselist(data)
    }
    let data = {
      page:this.data.page,
      size:this.data.size
    }
    this.setData({
    	page:this.data.page+1
    })
    this.chooselist(data)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})